<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class ProductInclude extends Eloquent
{
	use SoftDeletingTrait;

	public $timestamps   = true;
	protected $dates     = ['deleted_at'];
	protected $table     = 'product_include';
	protected $validator = null;

	public function saveData($data)
	{
		if(!$product_include = $this->checkExists($data['product'], $data['include'])){
			$product_include = new ProductInclude;
		}

		$product_include->product  = $data['product'];
		$product_include->include  = $data['include'];
		$product_include->quantity = $data['quantity'];

		$product_include->save();

		return $product_include;
	}

	public function scopeProductIncludes($query, $product){
		$resultNotIn = $query->select('include')->whereProduct($product)->get()->toArray();

		if(count($resultNotIn) > 0){
			$result = DB::table('product as p')
						->join('taxonomy as tb', 'tb.id', '=', 'p.brand')
						->join('taxonomy as tt', 'tt.id', '=', 'p.type')
						->select('p.id', 'p.name as name', 'p.net', 'tb.taxonomy as brand', 'tt.taxonomy as type')
						->where('p.deleted_at', '=', NULL)
						->where('p.id', '<>', $product)
						->whereNotIn('p.id', $resultNotIn)
						->orderBy('tb.order')
						->orderBy('p.net')
						->orderBy('p.name')
						->get();
		}else{
			$result = DB::table('product as p')
						->join('taxonomy as tb', 'tb.id', '=', 'p.brand')
						->join('taxonomy as tt', 'tt.id', '=', 'p.type')
						->select('p.id', 'p.name as name', 'p.net', 'tb.taxonomy as brand', 'tt.taxonomy as type')
						->where('p.deleted_at', '=', NULL)
						->where('p.id', '<>', $product)
						->orderBy('tb.order')
						->orderBy('p.net')
						->orderBy('p.name')
						->get();
		}

		return $result;
	}

	public function scopeSelectedProducts($query, $product){
		$result = DB::table('product_include as pi')
						->join('product as p', 'pi.include', '=', 'p.id')
						->join('taxonomy as tb', 'tb.id', '=', 'p.brand')
						->join('taxonomy as tt', 'tt.id', '=', 'p.type')
						->select('pi.id', 'pi.quantity', 'pi.include', 'p.id as product', 'p.name as name', 'p.net', 'tb.taxonomy as brand', 'tt.taxonomy as type')
						->where('pi.deleted_at', '=', NULL)
						->where('pi.product', '=', $product)
						->orderBy('tb.order')
						->get();

		return $result;
	}

	public function validator($data)
	{
		$rules = array(
				'quantity' => 
					array(
						'required',
						'numeric',
						'min:1',
					),
				);

		$this->validator = Validator::make($data, $rules);

		return $this->validator;
	}

	public function getValidatorMessage(){
		$messages = $this->validator->messages();
		
		$result   = array();

		foreach($messages->all() as $m){
			$result[]['message'] = $m;
		}

		return $result;
	}

	public function checkExists($product, $include){
		$data = DB::table('product_include')->where('product', '=', $product)->where('include', '=', $include)->first();

		$id = (isset($data->id))? $data->id : 0;

		$result = false;

		if($id != NULL && $id > 0){
			$result = ProductInclude::find($id);
		}

		return $result;
	}
}